package com.ncloud.crm.business;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.crm.service.BsTrialService;
import com.ncloud.msg.dubboservice.MailDubboService;
import com.ncloud.param.service.TagService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by shiqiang on 2017/6/14.
 */
@Service
public class TrialBusi {
    private static final Logger logger = LogManager.getLogger(TrialBusi.class);
    @Autowired
    private BsTrialService bsTrialService;
    @Reference
    private MailDubboService mailDubboService;
    @Reference
    private TagService tagService;
    @Transactional
    public JSONObject saveTrialInfo(JSONObject json) {
        /*存储信息*/
        String tag = json.containsKey("cloudtag")?json.getString("cloudtag"):"0";
        if("1".equals(tag)){
            json.put("contactEmail","-");
            json.put("remark","京东");
        }
        JSONObject content = bsTrialService.insertTrial(json);
        JSONObject tagParam = new JSONObject();
        tagParam.put("tagCode","trial_manager_email");
        JSONArray tagArray = tagService.getTagInfo(tagParam);
        String tagInfo = tagArray.getJSONObject(0).getString("tagInfo");
        /*开通内容*/
        if(content.containsKey("hasNetManager") && "1".equals(content.getString("hasNetManager"))){
            content.put("hasNetManager","是");
        }else{
            content.put("hasNetManager","否");
        }
        logger.info("试用信息："+content.toJSONString());
        JSONObject email = new JSONObject();
        email.put("sysCode", "trial");
        email.put("msgTo", tagInfo);
        email.put("subject", "试用申请");
        email.put("content", content);
        if("1".equals(tag)){
            email.put("templateName", "jdCustTrial.ftl");
        }else {
            email.put("templateName", "custTrial.ftl");
        }
        email.put("msgType", "1");
        mailDubboService.insertEmail(email);
        return new JSONObject();
    }
}
